<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>微博首页</h1>
<div>
    <div v-if="v.user==''"><!--用户未登录-->
        <a href="/reg.html">注册</a>
        <a href="/login.html">登录</a>
    </div>
    <div v-if="v.user!=''"><!--用户已登录-->
        <h1>欢迎{{weibo.nick}}</h1>
        <a href="/send.html">发布微博</a>
        <a href="/myweibo.html">我的微博</a>
        <a href="javascript:void (0)" @click="logout()">登出</a>
    </div>
    <hr>
    <div v-for="weibo in arr">
        <h3>
            <a :href="'/detail.html?id='+weibo.id">
                {{weibo.author}}:{{weibo.title}}
            </a>
            <a :href="'/detail.html?id='+weibo.id">
                <img :src="weibo.url" style="width: 200px" alt="">
            </a>
        </h3>
    </div>
</div>
</body>
<!--引入axios和vue-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
    let v = new Vue({
        el: "form",
        data: {
            user: "",
            arr: [],
        },
        created: function () {
            //获取当前登录的用户对象
            axios.get("/currentUser").then(function (response) {
                v.user = response.data;
            })
            //获取所有微博数据
            axios.get("/weibo/select").then(function (response) {
                v.arr = response.data
            })
        },
        methods: {
            logout: function () {
                if (confirm("您确认要退出吗?")) {
                    axios.get("/logout").then(function () {
                        location.reload();
                    })
                }
            }
        }
    })
</script>
</html>